package com.pktechan.api.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pktechan.api.entity.CustBase;
import com.pktechan.api.entity.TradeOrder;
import com.pktechan.api.entity.TradeOrderDetail;
import com.pktechan.api.enumeration.ChangeTradeOrderStatusEnum;
import com.pktechan.api.enumeration.TradeOrderStatusEnum;
import com.pktechan.api.exception.CustomException;
import com.pktechan.api.service.RedisService;
import com.pktechan.api.service.TradeOrderDetailService;
import com.pktechan.api.service.TradeOrderService;
import com.pktechan.api.tradeOrderStatus.BaseChangeTradeOrderState;
import com.pktechan.api.tradeOrderStatus.ChangeTradeOrderState;
import com.pktechan.api.utils.HtmlUtil;
import com.pktechan.api.vo.TradeOrderStatusVO;
import com.pktechan.api.vo.TradeOrderVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @description: 订单相关功能
 * @author: s.zhang
 * @create: 2021-03-05 16:06
 **/
@RestController
@RequestMapping("tradeOrderController")
public class TradeOrderController {
    private static final Logger logger = LoggerFactory.getLogger(TradeOrderController.class);

    @Autowired
    private TradeOrderService tradeOrderService;
    @Autowired
    private TradeOrderDetailService tradeOrderDetailService;
    @Autowired
    private RedisService redisService;

    /**
     * 根据id查询订单表
     *
     * @param orderId
     * @param response
     */
    @RequestMapping(value = "getTradeOrderById", method = RequestMethod.POST)
    public void getTradeOrderById(@RequestParam Integer orderId, HttpServletResponse response) {
        try {
            logger.info("根据id=" + orderId + "查询状态流水表");
            TradeOrder tradeOrder = tradeOrderService.getById(orderId);
            HtmlUtil.writerSuccessJson(response, tradeOrder);
        } catch (CustomException e) {
            e.printStackTrace();
            logger.debug(e.getErrMsg());
            HtmlUtil.writerFailJson(response, e.getErrMsg());
        } catch (Exception e) {
            e.printStackTrace();
            logger.debug(e.getMessage());
            HtmlUtil.writerFailJson(response, e.getMessage());
        }
    }

    /**
     * 查询订单表列表
     *
     * @param tradeOrder
     * @param response
     */
    @RequestMapping(value = "getTradeOrderList", method = RequestMethod.POST)
    public void getTradeOrderList(@RequestBody TradeOrder tradeOrder, HttpServletRequest request, HttpServletResponse response) {
        try {
            logger.info("查询订单表列表");
            PageHelper.startPage(tradeOrder.getPageNo(), tradeOrder.getPageSize());
            List<TradeOrderVO> list = tradeOrderService.getTradeOrderList(tradeOrder);
            PageInfo pageInfo = new PageInfo(list);
            HtmlUtil.writerSuccessJson(response, pageInfo);
        } catch (CustomException e) {
            e.printStackTrace();
            logger.debug(e.getErrMsg());
            HtmlUtil.writerFailJson(response, e.getErrMsg());
        } catch (Exception e) {
            e.printStackTrace();
            logger.debug(e.getMessage());
            HtmlUtil.writerFailJson(response, e.getMessage());
        }
    }

    /**
     * 添加到订单表
     *
     * @param tradeOrder
     * @param response
     */
    @RequestMapping(value = "saveTradeOrder", method = RequestMethod.POST)
    public void saveTradeOrder(@RequestBody TradeOrder tradeOrder, HttpServletResponse response) {
        try {
            logger.info("添加到订单表");
            tradeOrderService.save(tradeOrder);
            HtmlUtil.writerSuccessJson(response);
        } catch (CustomException e) {
            e.printStackTrace();
            logger.debug(e.getErrMsg());
            HtmlUtil.writerFailJson(response, e.getErrMsg());
        } catch (Exception e) {
            e.printStackTrace();
            logger.debug(e.getMessage());
            HtmlUtil.writerFailJson(response, e.getMessage());
        }
    }

    /**
     * 根据Id订单表
     *
     * @param tradeOrder
     * @param response
     */
    @RequestMapping(value = "updateTradeOrder", method = RequestMethod.POST)
    public void updateTradeOrder(@RequestBody TradeOrder tradeOrder, HttpServletResponse response) {
        try {
            logger.info("根据id=" + tradeOrder.getId() + "单表");
            tradeOrderService.update(tradeOrder);
            HtmlUtil.writerSuccessJson(response);
        } catch (CustomException e) {
            e.printStackTrace();
            logger.debug(e.getErrMsg());
            HtmlUtil.writerFailJson(response, e.getErrMsg());
        } catch (Exception e) {
            e.printStackTrace();
            logger.debug(e.getMessage());
            HtmlUtil.writerFailJson(response, e.getMessage());
        }
    }


    /**
     * 根据id查询订单表详情
     *
     * @param orderDetailId
     * @param response
     */
    @RequestMapping(value = "getTradeOrderDetailById", method = RequestMethod.POST)
    public void getTradeOrderDetailById(@RequestParam Integer orderDetailId, HttpServletResponse response) {
        try {
            logger.info("根据id=" + orderDetailId + "查询状态流水表详情");
            TradeOrderDetail tradeOrderDetail = tradeOrderDetailService.getById(orderDetailId);
            HtmlUtil.writerSuccessJson(response, tradeOrderDetail);
        } catch (CustomException e) {
            e.printStackTrace();
            logger.debug(e.getErrMsg());
            HtmlUtil.writerFailJson(response, e.getErrMsg());
        } catch (Exception e) {
            e.printStackTrace();
            logger.debug(e.getMessage());
            HtmlUtil.writerFailJson(response, e.getMessage());
        }
    }

    /**
     * 查询订单详情表列表
     *
     * @param tradeOrderDetail
     * @param response
     */
    @RequestMapping(value = "getTradeOrderDetailList", method = RequestMethod.POST)
    public void getTradeOrderDetailList(@RequestBody TradeOrderDetail tradeOrderDetail, HttpServletResponse response) {
        try {
            logger.info("查询订单详情表列表");
            PageHelper.startPage(tradeOrderDetail.getPageNo(), tradeOrderDetail.getPageSize());
            List<TradeOrderDetail> list = tradeOrderDetailService.getList(tradeOrderDetail);
            PageInfo pageInfo = new PageInfo(list);
            HtmlUtil.writerSuccessJson(response, pageInfo);
        } catch (CustomException e) {
            e.printStackTrace();
            logger.debug(e.getErrMsg());
            HtmlUtil.writerFailJson(response, e.getErrMsg());
        } catch (Exception e) {
            e.printStackTrace();
            logger.debug(e.getMessage());
            HtmlUtil.writerFailJson(response, e.getMessage());
        }
    }

    /**
     * 添加到订单详情表
     *
     * @param tradeOrderDetail
     * @param response
     */
    @RequestMapping(value = "saveTradeOrderDetail", method = RequestMethod.POST)
    public void saveTradeOrderDetail(@RequestBody TradeOrderDetail tradeOrderDetail, HttpServletResponse response) {
        try {
            logger.info("添加到订单详情表");
            tradeOrderDetailService.save(tradeOrderDetail);
            HtmlUtil.writerSuccessJson(response);
        } catch (CustomException e) {
            e.printStackTrace();
            logger.debug(e.getErrMsg());
            HtmlUtil.writerFailJson(response, e.getErrMsg());
        } catch (Exception e) {
            e.printStackTrace();
            logger.debug(e.getMessage());
            HtmlUtil.writerFailJson(response, e.getMessage());
        }
    }

    /**
     * 根据Id订单表情表
     *
     * @param tradeOrderDetail
     * @param response
     */
    @RequestMapping(value = "updateTradeOrderDetailById", method = RequestMethod.POST)
    public void updateTradeOrderDetailById(@RequestBody TradeOrderDetail tradeOrderDetail, HttpServletResponse response) {
        try {
            logger.info("根据id=" + tradeOrderDetail.getId() + "单表情表");
            tradeOrderDetailService.update(tradeOrderDetail);
            HtmlUtil.writerSuccessJson(response);
        } catch (CustomException e) {
            e.printStackTrace();
            logger.debug(e.getErrMsg());
            HtmlUtil.writerFailJson(response, e.getErrMsg());
        } catch (Exception e) {
            e.printStackTrace();
            logger.debug(e.getMessage());
            HtmlUtil.writerFailJson(response, e.getMessage());
        }
    }


    /**
     * 复购物车产品下订单
     *
     * @param shopCartIdList
     * @param custAddressId
     */
    @RequestMapping(value = "repurchasePlaceOrder", method = RequestMethod.POST)
    public void repurchasePlaceOrder(@RequestParam List<Integer> shopCartIdList, @RequestParam Integer custAddressId, HttpServletRequest request, HttpServletResponse response) {
        try {
            CustBase custBase = redisService.getCustBaseByUserLoginState(request.getParameter("userLoginState"));
            TradeOrder tradeOrder = tradeOrderService.repurchasePlaceOrder(shopCartIdList, custAddressId, custBase);
            HtmlUtil.writerSuccessJson(response, tradeOrder);
        } catch (CustomException e) {
            e.printStackTrace();
            logger.debug(e.getErrMsg());
            HtmlUtil.writerFailJson(response, e.getErrMsg());
        } catch (Exception e) {
            e.printStackTrace();
            logger.debug(e.getMessage());
            HtmlUtil.writerFailJson(response, e.getMessage());
        }
    }

    /**
     * 完成订单
     */
    @RequestMapping(value = "completeOrder", method = RequestMethod.POST)
    public void completeOrder(@RequestParam Integer tradeOrderId, HttpServletRequest request, HttpServletResponse response) {
        try {
            CustBase custBase = redisService.getCustBaseByUserLoginState(request.getParameter("userLoginState"));
            //记录操作流水
            ChangeTradeOrderState changeTradeOrderState = BaseChangeTradeOrderState.getChangeContractState(ChangeTradeOrderStatusEnum.COMPLETE_ORDER);
            changeTradeOrderState.doChange(tradeOrderId, TradeOrderStatusEnum.IN_DELIVERY.getCode(), TradeOrderStatusEnum.COMPLETED.getCode(), custBase.getId(), ChangeTradeOrderStatusEnum.COMPLETE_ORDER.getName());
            HtmlUtil.writerSuccessJson(response);
        } catch (CustomException e) {
            e.printStackTrace();
            logger.debug(e.getErrMsg());
            HtmlUtil.writerFailJson(response, e.getErrMsg());
        } catch (Exception e) {
            e.printStackTrace();
            logger.debug(e.getMessage());
            HtmlUtil.writerFailJson(response, e.getMessage());
        }
    }

    /**
     * 根据custId和订单状态查询订单
     *
     * @param tradeOrder
     * @param response
     */
    @RequestMapping(value = "getTradeOrderListByCustIdAndStatus", method = RequestMethod.POST)
    public void getTradeOrderListByCustIdAndStatus(@RequestBody TradeOrderVO tradeOrder, HttpServletResponse response) {
        try {
            logger.info("根据custId和订单状态查询订单");
            List<TradeOrderVO> tradeOrderVOList = tradeOrderService.getTradeOrderListByCustIdAndStatus(tradeOrder);
            HtmlUtil.writerSuccessJson(response, tradeOrderVOList);
        } catch (CustomException e) {
            e.printStackTrace();
            logger.debug(e.getErrMsg());
            HtmlUtil.writerFailJson(response, e.getErrMsg());
        } catch (Exception e) {
            e.printStackTrace();
            logger.debug(e.getMessage());
            HtmlUtil.writerFailJson(response, e.getMessage());
        }
    }

    /**
     * 接受订单
     */
    @RequestMapping(value = "acceptOrder", method = RequestMethod.POST)
    public void acceptOrder(@RequestParam Integer tradeOrderId, HttpServletRequest request, HttpServletResponse response) {
        try {
            CustBase custBase = redisService.getCustBaseByUserLoginState(request.getParameter("userLoginState"));
            //记录操作流水
            ChangeTradeOrderState changeTradeOrderState = BaseChangeTradeOrderState.getChangeContractState(ChangeTradeOrderStatusEnum.DELIVERY_ORDER);
            changeTradeOrderState.doChange(tradeOrderId, TradeOrderStatusEnum.ORDERS_TO_BE_RECEIVED.getCode(), TradeOrderStatusEnum.IN_DELIVERY.getCode(), custBase.getId(), ChangeTradeOrderStatusEnum.DELIVERY_ORDER.getName());
            HtmlUtil.writerSuccessJson(response);
        } catch (CustomException e) {
            e.printStackTrace();
            logger.debug(e.getErrMsg());
            HtmlUtil.writerFailJson(response, e.getErrMsg());
        } catch (Exception e) {
            e.printStackTrace();
            logger.debug(e.getMessage());
            HtmlUtil.writerFailJson(response, e.getMessage());
        }
    }

    /**
     * 订单转让
     *
     * @param tradeOrder
     * @param response
     */
    @RequestMapping(value = "orderTransfer", method = RequestMethod.POST)
    public void orderTransfer(@RequestBody TradeOrderVO tradeOrder, HttpServletResponse response) {
        try {
            logger.info("根据id=" + tradeOrder.getId() + "订单转让 新配送中心ID=" + tradeOrder.getDistributionCenterId() + "原配送中心ID=" + tradeOrder.getOldDistributionCenterId());
            tradeOrderService.orderTransfer(tradeOrder);
            HtmlUtil.writerSuccessJson(response);
        } catch (CustomException e) {
            e.printStackTrace();
            logger.debug(e.getErrMsg());
            HtmlUtil.writerFailJson(response, e.getErrMsg());
        } catch (Exception e) {
            e.printStackTrace();
            logger.debug(e.getMessage());
            HtmlUtil.writerFailJson(response, e.getMessage());
        }
    }


    /**
     * 根据custId查询订单状态数量
     *
     * @param tradeOrder
     * @param response
     */
    @RequestMapping(value = "getTradeOrderCountByCustId", method = RequestMethod.POST)
    public void getTradeOrderCountByCustId(@RequestBody TradeOrderVO tradeOrder, HttpServletResponse response) {
        try {
            logger.info("根据custId查询订单状态数量");
            List<TradeOrderStatusVO> tradeOrderStatusVOList = tradeOrderService.getTradeOrderCountByCustId(tradeOrder);
            HtmlUtil.writerSuccessJson(response, tradeOrderStatusVOList);
        } catch (CustomException e) {
            e.printStackTrace();
            logger.debug(e.getErrMsg());
            HtmlUtil.writerFailJson(response, e.getErrMsg());
        } catch (Exception e) {
            e.printStackTrace();
            logger.debug(e.getMessage());
            HtmlUtil.writerFailJson(response, e.getMessage());
        }
    }

    /**
     * 复购产品下订单
     *
     * @param productId
     * @param custAddressId
     * @param count
     */
    @RequestMapping(value = "placeAnOrder", method = RequestMethod.POST)
    public void placeAnOrder(@RequestParam Integer productId, @RequestParam Integer custAddressId, @RequestParam Integer count, HttpServletRequest request, HttpServletResponse response) {
        try {
            CustBase custBase = redisService.getCustBaseByUserLoginState(request.getParameter("userLoginState"));
            TradeOrder tradeOrder = tradeOrderService.placeAnOrder(productId, custAddressId, count, custBase);
            HtmlUtil.writerSuccessJson(response, tradeOrder);
        } catch (CustomException e) {
            e.printStackTrace();
            logger.debug(e.getErrMsg());
            HtmlUtil.writerFailJson(response, e.getErrMsg());
        } catch (Exception e) {
            e.printStackTrace();
            logger.debug(e.getMessage());
            HtmlUtil.writerFailJson(response, e.getMessage());
        }
    }


    /**
     * 获取直接推荐人下的订单集合
     *
     * @param response
     */
    @RequestMapping(value = "/getTradeOrderVOByCustId", method = RequestMethod.POST)
    public void getTradeOrderVOByCustId(@RequestParam Integer custId, HttpServletRequest request, HttpServletResponse response) {
        try {
            logger.warn("根据custId=" + custId + "获取订单集合");
            List<TradeOrderVO> tradeOrderVOList = tradeOrderService.getTradeOrderVOByCustId(custId);
            HtmlUtil.writerSuccessJson(response, tradeOrderVOList);
        } catch (CustomException e) {
            e.printStackTrace();
            logger.debug(e.getErrMsg());
            HtmlUtil.writerFailJson(response, e.getErrMsg());
        } catch (Exception e) {
            e.printStackTrace();
            logger.debug(e.getMessage());
            HtmlUtil.writerFailJson(response, e.getMessage());
        }
    }


    /**
     * 获取间接推荐人下的订单集合
     *
     * @param response
     */
    @RequestMapping(value = "/getTradeOrderVOByYyId", method = RequestMethod.POST)
    public void getTradeOrderVOByYyId(@RequestParam Integer yyId, HttpServletRequest request, HttpServletResponse response) {
        try {
            logger.warn("根据yyId=" + yyId + "获取当前间接推荐人下的订单集合");
            List<TradeOrderVO> tradeOrderVOList = tradeOrderService.getTradeOrderVOByYyId(yyId);
            HtmlUtil.writerSuccessJson(response, tradeOrderVOList);
        } catch (CustomException e) {
            e.printStackTrace();
            logger.debug(e.getErrMsg());
            HtmlUtil.writerFailJson(response, e.getErrMsg());
        } catch (Exception e) {
            e.printStackTrace();
            logger.debug(e.getMessage());
            HtmlUtil.writerFailJson(response, e.getMessage());
        }
    }

    /**
     * 获取待我配送的订单数量
     * @param custId
     * @param request
     * @param response
     */
    @RequestMapping(value = "/getMeSendingOrderCnt", method = RequestMethod.POST)
    public void getMeSendingOrderCnt(@RequestParam Integer custId, HttpServletRequest request, HttpServletResponse response){
        logger.warn("根据配送商ID=" + custId + "获取待此人发货中的订单数量");
        Integer sendingCnt = tradeOrderService.getMeSendingOrderCnt(custId);
        HtmlUtil.writerSuccessJson(response, sendingCnt);
    }
}
